package cn.slipi.admin.common.annotation.time;

import cn.slipi.admin.common.utils.DateUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * 运行时间
 *
 * @author lee
 * @date 2019-03-21
 * @since 1.0.0
 */

@Component
@Aspect
public class UsedTimeAspect {
    private static final Logger LOG = LoggerFactory.getLogger(UsedTimeAspect.class);

    public UsedTimeAspect() {
    }

    @Pointcut("@annotation(cn.slipi.admin.common.annotation.time.UsedTime)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object aroundHandle(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String methodName = method.getName();
        long startTime = System.currentTimeMillis();
        Object obj = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        LOG.info("当前时间:" + DateUtil.getFormatDate(new Date()) + ",方法:" + methodName + "(),当前消耗时间:" + (endTime - startTime) + "ms," + (endTime - startTime) / 1000L + "s");
        return obj;
    }
}
